﻿using AutoMapper;
using Microsoft.Data.SqlClient;
using NetTaste;
using Newtonsoft.Json.Linq;
using SqlSugar;
using System.Data;
using System.Linq.Expressions;
using System.Numerics;
using System.Security.Cryptography;
using System.Text.RegularExpressions;
using VM.Common.DataUntil;

using VM.Common.Result;
using VM.DAL.System.Code;
using VM.Entity.EntityDto.People;
using VM.Entity.EntityMap.Member;
using VM.Entity.EntityMap.Menu;
using VM.Entity.EntityMap.MsgCode;
using VM.Entity.EntityMap.People;
using VM.Entity.EntityMap.Token;
using VM.IBLL.Menu;
using VM.IBLL.People;
using VM.IDAL;
using VM.IDAL.People;
using static Dm.net.buffer.ByteArrayBuffer;

namespace VM.BLL.People
{
    public class PeoPleInfoBLL : BaseBLL, IPeopleInfoBLL
    {

        private readonly ISqlSuggarDAL _ISqlSuggarDAL;
        private readonly IPeoPleInfoDAL _IPeoPleInfoDAL;
        private readonly IMapper _IMapper;
        public PeoPleInfoBLL(ISqlSugarClient client, ISqlSuggarDAL sqlSuggarDAL, IPeoPleInfoDAL iPeoPleInfoDAL, IMapper imapper)
        {
            _ISqlSuggarDAL = sqlSuggarDAL;
            _IPeoPleInfoDAL = iPeoPleInfoDAL;
            _IMapper = imapper;
        }

        public ApiResult addPeopleInfo(addPeopleInfoDto obj)
        {
            ApiResult res = new ApiResult();
            people_info peopleInfo = _IMapper.Map<addPeopleInfoDto, people_info>(obj);
            //处理表头
            int addRes = _IPeoPleInfoDAL.addPeoPleInfo(peopleInfo);
            if (addRes>0)
            {
                res.code = 201;
                res.msg = $@"添加成功{addRes}条数据";
            }
            else
            {
                res.code = 403;
                res.msg = "添加失败";
            }
            return res;
        }


        public ApiDataResult<getPeopleInfoDetailDto> getPeopleInfoById(int? userId, int id)
        {


            getPeopleInfoDetailDto peopleInfo = _IPeoPleInfoDAL.getPeopleInfoDetailById(id);
            ApiDataResult<getPeopleInfoDetailDto> res = new ApiDataResult<getPeopleInfoDetailDto>();
            if (peopleInfo.id ==id)
            {
                res.code = 200;
                res.msg = "获取成功";
                res.data = peopleInfo;

            }
            else
            {
                res.code = 200;
                res.msg = "无数据";
                res.data = null;
            }

            return res;
        }

        public ApiDataResult<PagingData<getPeopleInfoListDto>> getPeopleInfoList(int pageIndex,int pageSize,int? userId, string? name,int typeId)
        {
            PagingData<getPeopleInfoListDto> peopleInfoList = _IPeoPleInfoDAL.getPeopleInfoList(pageIndex,pageSize, name, typeId, (int)userId);
           
          ApiDataResult<PagingData<getPeopleInfoListDto>> res = new ApiDataResult<PagingData<getPeopleInfoListDto>>() {
           code = 200,
           msg = "获取成功",
           data = peopleInfoList

          };
         return res;
        }

        public ApiResult updatePeopleInfo(updatePeopleInfoDto obj)
        {
            ApiResult res = new ApiResult();
            people_info peopleInfo = _IMapper.Map<updatePeopleInfoDto, people_info>(obj);
            //处理表头
            int addRes = _IPeoPleInfoDAL.updatePeoPleInfo(peopleInfo);
            if (addRes > 0)
            {
                res.code = 201;
                res.msg = $@"成功修改{addRes}条数据";
            }
            else
            {
                res.code = 403;
                res.msg = "修改失败";
            }
            return res;
        }

        /// <summary>
        /// 删除接口
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public ApiResult deletePeoPleInfo(int id)
        {
            ApiResult res = new ApiResult();
    
            int addRes = _IPeoPleInfoDAL.deletePeoPleInfo(id);
            if (addRes > 0)
            {
                res.code = 201;
                res.msg = $@"删除{addRes}条数据";
            }
            else
            {
                res.code = 403;
                res.msg = "删除失败";
            }
            return res;

        }

    }



}
